package com.xiaoliang.servlet.cart;

import com.xiaoliang.entry.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author XIAOLIANG
 * @ClassName DeleteProductFromCart.java
 * @Description TODO
 * @createTime 2021年02月26日 18:44:00
 */
//@WebServlet("/cart/delete")
public class DeleteProductFromCart extends HttpServlet {


	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Integer id = Integer.valueOf(req.getParameter("id"));

		HttpSession session = req.getSession();
		HashMap<Integer,Product> cart = (HashMap<Integer, Product>) session.getAttribute("cart");

		if (cart.containsKey(id)){
			session.setAttribute("msg",cart.get(id).getName()+"已经从购物车中删除");
			cart.remove(id);
		}else {
			session.setAttribute("msg","删除失败 ,该商品已经不存在购物车中");
		}


		resp.sendRedirect(req.getContextPath()+"/cart/cartList.jsp");

	}
}
